package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {
    @Autowired
    ItemCatMapper itemCatMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ProductConverter productConverter;

    /**
     * @author: 小朱
     * @Description: 通过商品的唯一id查询商品详情
     * @date: 16:35 2022/12/4
     */
    @Override
    public ProductDetailDto getProductDetail(Long id) {
        //通过id来查询item表
        Item item = itemMapper.selectByPrimaryKey(id);
        //通过id来查询itemDesc表
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item, itemDesc);
        return productDetailDto;
    }

    /**
     * @author: 小朱
     * @Description: 通过PageRequest类分页查询商品列表
     * @date: 16:36 2022/12/4
     */
    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {
        String orderCol = null;
        String orderDir = null;
        String sort = pageInfo.getSort();
        if (sort == null || "".equals(sort)) {
            orderCol = "created";
            orderDir = "desc";
        } else if ("1".equals(sort)) {
            orderCol = "price";
            orderDir = "asc";
        } else if ("-1".equals(sort)) {
            orderCol = "price";
            orderDir = "desc";
        }
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getSize());
        Integer priceGt = pageInfo.getPriceGt();
        Integer priceLte = pageInfo.getPriceLte();
        List<Item> items = itemMapper.selectItemFront(null, orderCol, orderDir, priceGt, priceLte);
        List<ProductDto> productDtos = productConverter.items2Dto(items);
        PageInfo<Item> pageInfo1 = new PageInfo<>(items);
        AllProductResponse allProductResponse = new AllProductResponse();
        allProductResponse.setData(productDtos);
        allProductResponse.setTotal(pageInfo1.getTotal());
        return allProductResponse;

    }


    @Override
    public Set<PanelDto> getRecommendGoods() {
        return null;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }
}


//
